local skel = fk.CreateSkill {
  name = "lb__panyao",
  max_branches_use_time = {
    ["slash"] = {
      [Player.HistoryRound] = 1
    },
    ["duel"] = {
      [Player.HistoryRound] = 1
    },
    ["nullification"] = {
      [Player.HistoryRound] = 1
    },
  }
}

local function utf8sub(str, startChar, numChars)
  local startIndex = 1
  while startChar > 1 do
    local char = string.byte(str, startIndex)
    startIndex = startIndex + (char > 240 and 4 or char > 225 and 3 or char > 192 and 2 or 1)
    startChar = startChar - 1
  end
  local currentIndex = startIndex
  while numChars > 0 and currentIndex <= #str do
    local char = string.byte(str, currentIndex)
    currentIndex = currentIndex + (char > 240 and 4 or char > 225 and 3 or char > 192 and 2 or 1)
    numChars = numChars - 1
  end
  return str:sub(startIndex, currentIndex - 1)
end


Fk:addQmlMark {
  name = "lb__panyao",
  how_to_show = function(_, value, player)
    if type(value) ~= "table" then return " " end
    local list = { "slash", "duel", "nullification" }
    local mark = ""
    for i, num in ipairs(value) do
      local name = utf8sub(Fk:translate(list[i]), 1, 1)
      name = name .. tostring(num)
      local color = "#0598BC"
      if skel:withinBranchTimesLimit(player, list[i], Player.HistoryRound) then
        color = "#FFFFFF"
      end
      mark = mark .. "<font color='" .. color .. "'>" .. name .. " </font>"
    end
    return mark
  end,
}

Fk:loadTranslationTable {
  ["lb__panyao"] = "蟠逍",
  [":lb__panyao"] = "每轮每牌名限一次，你可以将三/两/一张牌当【无懈可击】/【决斗】/【杀】使用或打出并令下次转化对应牌所需牌数-1（为0则改为视为）。",

  ["#lb__panyao"] = "蟠逍：你可以将三/两/一张牌当【无懈可击】/【决斗】/【杀】使用或打出",
  ["#lb__panyao-ask"] = "蟠逍：你可以将 %arg 张牌当【%arg2】使用或打出",
  ["#lb__panyao-virtual"] = "蟠逍：你可以视为使用或打出【%arg】",

  ["@[lb__panyao]lb__panyao"] = "蟠逍",

  ["$lb__panyao1"] = "生死虚实，一念之间。",
  ["$lb__panyao2"] = "洞天幻化，长梦一觉…",
  ["$lb__panyao3"] = "就是现在！",
}

skel:addEffect("viewas", {
  handly_pile = true,
  pattern = "slash,duel,nullification",
  prompt = function(self, player, selected_cards, selected)
    if self.interaction.data then
      local all = { "slash", "duel", "nullification" }
      local num = player:getTableMark("@[lb__panyao]lb__panyao")[table.indexOf(all, self.interaction.data)]
      if num == 0 then
        return "#lb__panyao-virtual:::" .. self.interaction.data
      end
      return "#lb__panyao-ask:::" .. num .. ":" .. self.interaction.data
    end
    return "#lb__panyao"
  end,
  interaction = function(self, player)
    local all = { "slash", "duel", "nullification" }
    local list = table.filter(player:getViewAsCardNames(skel.name, all), function(name)
      return skel:withinBranchTimesLimit(player, name, Player.HistoryRound)
    end)
    if #list > 0 then
      return UI.CardNameBox { choices = list, all_choices = all }
    end
  end,
  card_filter = function(self, player, to_select, selected, selected_targets)
    if self.interaction.data then
      local num = player:getTableMark("@[lb__panyao]lb__panyao")[table.indexOf({ "slash", "duel", "nullification" }, self.interaction.data)]
      return num and #selected < num
    end
  end,
  view_as = function(self, player, cards)
    if self.interaction.data then
      local all = { "slash", "duel", "nullification" }
      local num = table.indexOf(all, self.interaction.data)
      if #cards == player:getTableMark("@[lb__panyao]lb__panyao")[num] then
        local card = Fk:cloneCard(all[num])
        card:addSubcards(cards)
        card.skillName = skel.name
        return card
      end
    end
  end,
  before_use = function(self, player, use)
    if self.interaction.data then
      player:addSkillBranchUseHistory(skel.name, self.interaction.data)
      local all = { "slash", "duel", "nullification" }
      local num = table.indexOf(all, self.interaction.data)
      local list = player:getTableMark("@[lb__panyao]lb__panyao")
      list[num] = math.max(((list[num] or 0) - 1), 0)
      player.room:setPlayerMark(player, "@[lb__panyao]lb__panyao", list)
    end
  end,
  enabled_at_play = function(self, player)
    return table.find(player:getViewAsCardNames(skel.name, { "slash", "duel", "nullification" }), function(name)
      return skel:withinBranchTimesLimit(player, name, Player.HistoryRound)
    end)
  end,
  enabled_at_response = function(self, player, response)
    return self:enabledAtPlay(player)
  end,
})

skel:addAcquireEffect(function(self, player, is_start)
  player.room:setPlayerMark(player, "@[lb__panyao]lb__panyao", { 1, 2, 3 })
end)

skel:addLoseEffect(function(self, player, is_death)
  player.room:setPlayerMark(player, "@[lb__panyao]lb__panyao", 0)
end)

return skel
